import requests
from lxml import etree
import time
import csv

fq=open('./guangzhouQfang.csv','w+',newline='',encoding='utf-8')
headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'}
writer=csv.writer(fq)
writer.writerow(('小区名称','户型','建筑面积','建筑年代','楼层','所在区域','租金'))

# url='https://guangzhou.qfang.com/rent/f1'
def get_info(url):
    html=requests.get(url,headers=headers)   #向服务器发出请求
    infors=etree.HTML(html.text)   #解析网页内容
    names=infors.xpath('//div[@class="list-main fl"]/div[3]/div[@class="text fl"]/a/text()') #小区名称
    house_types=infors.xpath('//div[@class="house-metas clearfix"]/p[1]/text()')   #户型
    areas=infors.xpath('//div[@class="house-metas clearfix"]/p[2]/text()')       #建筑面积
    floors=infors.xpath('//div[@class="house-metas clearfix"]/p[4]/text()')  #楼层
    regions=infors.xpath('//div[@class="list-main fl"]/div[3]/div[@class="text fl"]/text()')   #所在区域
    prices=infors.xpath('//div[@class="list-price"]/p/span[1]/text()')  #租金价格
    for name,floor,region,house_type,area,price in zip(names,floors,regions,house_types,areas,prices):
        name=name.strip()
        floor=floor.strip()
        region=region.strip()
        price=price+'元/月'
        writer.writerow((name,house_type,area,floor,region,price))
        print(name,house_type,area,floor,region,price)

if __name__=='__main__':
    urls=['https://guangzhou.qfang.com/rent/f{}'.format(str(i)) for i in range(1,100)]
    for url in urls:
        get_info(url)
        time.sleep(2)
    fq.close()
